package com.shuiqinling.ww.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

import com.shuiqinling.ww.android.Util.LebianHelper;
import com.shuiqinling.ww.android.cn00.R;
import com.shuiqinling.ww.android.qq.QQApi;
import com.tencent.tauth.Tencent;
import com.tendcloud.tenddata.TalkingDataGA;
import com.unity3d.player.UnityPlayer;

public class UnityPlayerActivity extends Activity implements OnClickListener {
	protected UnityPlayer mUnityPlayer; // don't change the name of this
										// variable; referenced from native code

	// Setup activity layout
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia
														// play happy

		mUnityPlayer = new UnityPlayer(this);
		setContentView(mUnityPlayer);
		mUnityPlayer.requestFocus();

		// Log.d("UnityPlayerActivity","UnityPlayerActivity success!");
		// AVOSCloud.initialize(this, "7QYJNkI4KRXoIIohTn9BxNWx-gzGzoHsz",
		// "3RcqYQdFjFrYgTibGMqrlvfQ");

		LebianHelper.queryUpdate(this);
		// 注册默认的消息处理逻辑
//		AVIMMessageManager.registerDefaultMessageHandler(new CustomMessageHandler());
//		AVIMMessageManager.setConversationEventHandler(new CustomConversationEventHandler());
//		AVIMClient.setClientEventHandler(new CustomClientEventHandler());

		AndroidDirectCall.Init(this);
	}

	// Quit Unity
	@Override
	protected void onDestroy() {
		mUnityPlayer.quit();
		super.onDestroy();
	}

	// Pause Unity
	@Override
	protected void onPause() {
		super.onPause();
		mUnityPlayer.pause();
		TalkingDataGA.onPause(this);
	}

	// Resume Unity
	@Override
	protected void onResume() {
		super.onResume();
		mUnityPlayer.resume();
		TalkingDataGA.onResume(this);
	}

	// This ensures the layout will be correct.
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mUnityPlayer.configurationChanged(newConfig);
	}

	// Notify Unity of the focus change.
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		mUnityPlayer.windowFocusChanged(hasFocus);
	}

	// For some reason the multiple keyevent type is not supported by the ndk.
	// Force event injection by overriding dispatchKeyEvent().
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
			return mUnityPlayer.injectEvent(event);
		return super.dispatchKeyEvent(event);
	}

	// Pass any events not handled by (unfocused) views straight to UnityPlayer
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			ShowQuitDialog();
			return true;
		}
		return mUnityPlayer.injectEvent(event);
	}

	private void ShowQuitDialog() {
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		builder.setTitle("魔女兵器")
			   .setIcon(R.drawable.app_icon)
			   .setMessage("确定要退出游戏")
			   .setNegativeButton("取消", this)
			   .setPositiveButton("确认", this)
			   .create().show();
		
	}
	@Override
	public void onClick(DialogInterface dialog, int which) {
		if(which==AlertDialog.BUTTON_POSITIVE){
			mUnityPlayer.quit();
			this.finish();
		}else{
			dialog.dismiss();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return mUnityPlayer.injectEvent(event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mUnityPlayer.injectEvent(event);
	}

	/* API12 */public boolean onGenericMotionEvent(MotionEvent event) {
		return mUnityPlayer.injectEvent(event);
	}

	//for tencent api
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Tencent.onActivityResultData(requestCode, resultCode, data, QQApi.getInstance(this));
	}



}
